﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web;

public class Functions
{
    /// <summary>
    /// 随机订单编号
    /// </summary>
    /// <returns></returns>
    public static string NewOrderNo()
    {
        Random r = new Random(GetRandomSeed());
        int i = r.Next(1, 999);
        string tick = i.ToString().PadLeft(3, '0');
        return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmssfff"), tick);
    }
    //获取随机种子
    static int GetRandomSeed()
    {
        byte[] bytes = new byte[4];
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        rng.GetBytes(bytes);
        return BitConverter.ToInt32(bytes, 0);
    }
    /// <summary>
    /// 随机验证码
    /// </summary>
    /// <returns></returns>
    public static string NewCode()
    {
        Random r = new Random(GetRandomSeed());
        int i = r.Next(1, 999999);
        string code = i.ToString().PadLeft(6, '0');
        return code;
    }

    /// <summary>
    /// encode编码，将空格(+)替换为20%
    /// </summary>
    /// <param name="str">要编码的字符串</param>
    /// <returns></returns>
    public static string UrlEncode(string str)
    {
        string UrlEncode = "";
        UrlEncode = HttpUtility.UrlEncode(str, Encoding.UTF8);
        UrlEncode = UrlEncode.Replace("+", "%20");
        return UrlEncode;
    }
    /// <summary>
    /// decode解码
    /// </summary>
    /// <param name="str">要解码的字符串</param>
    /// <returns></returns>
    public static string UrlDecode(string str)
    {
        return HttpUtility.UrlDecode(str, Encoding.UTF8);
    }
}
